<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="120px">
			<el-form-item label="档案区编号" prop="areaNo">
				<el-tree-select @change="changeArea" v-model="queryParams.areaNo" :data="areaOptions" :props="{ value: 'archAreaNo', label: 'label', children: 'children' }"
				 value-key="archAreaNo" placeholder="请选择档案区" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="档案柜" prop="cabinetNo">
				<el-tree-select v-model="queryParams.cabinetNo" :data="cabinetOptions" :props="{ value: 'colNo', label: 'label', children: 'children' }"
				 value-key="colNo" placeholder="请选择档案柜" clearable @keyup.enter="handleQuery" @click="getListArchiveCabinet" />
			</el-form-item>
			<el-form-item label="盒号" prop="boxCode">
				<el-input v-model="queryParams.boxCode" placeholder="请输入盒号" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<!-- <el-form-item label="档案盒条码" prop="boxRfid">
				<el-input v-model="queryParams.boxRfid" placeholder="请输入档案盒条码" clearable @keyup.enter="handleQuery" />
			</el-form-item> -->
			<el-form-item label="rfid" prop="rfid">
				<el-input v-model="queryParams.rfid" placeholder="请输入rfid" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="借阅状态" prop="giveBackStatus">
				<el-select v-model="queryParams.giveBackStatus" placeholder="请选择借阅状态">
					<el-option v-for="dict in borrow_flag" :key="dict.value" :label="dict.label" :value="dict.value">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="保管期限" prop="keepDur">
				<el-select v-model="queryParams.keepDur" placeholder="请选择保管期限">
					<el-option v-for="dict in keep_dur" :key="dict.value" :label="dict.label" :value="dict.value">
					</el-option>
				</el-select>
			</el-form-item>
			<!-- <el-form-item label="保密级别" prop="confdLv">
				<el-select v-model="queryParams.confdLv" placeholder="请选择保密级别">
					<el-option v-for="dict in confd_lv" :key="dict.value" :label="dict.label" :value="dict.value">
					</el-option>
				</el-select>
			</el-form-item> -->
			<!-- <el-form-item label="归档部门" prop="archDept">
				<el-input v-model="queryParams.archDept" placeholder="请输入归档部门" clearable @keyup.enter="handleQuery" />
			</el-form-item> -->

			<!-- 	<el-form-item label="管理单位" prop="mgtOrgNo">
				<el-input v-model="queryParams.mgtOrgNo" placeholder="请输入管理单位" clearable @keyup.enter="handleQuery" />
			</el-form-item> -->
			<!-- <el-form-item label="盒满标志" prop="boxFullFlag">
				<el-select v-model="queryParams.boxFullFlag" placeholder="请选择盒满标志">
					<el-option v-for="dict in box_full_flag" :key="dict.value" :label="dict.label" :value="dict.value">
					</el-option>
				</el-select>
			</el-form-item> -->
			<el-form-item label="用户编号" prop="custNo">
				<el-input v-model="queryParams.custNo" placeholder="请输入用户编号" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="用户名称" prop="custName">
				<el-input v-model="queryParams.custName" placeholder="请输入用户名称" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<!-- <el-form-item label="转存标志" prop="dumpFlag">
				<el-select v-model="queryParams.dumpFlag" placeholder="请选择盒满标志">
					<el-option v-for="dict in dump_flag" :key="dict.value" :label="dict.label" :value="dict.value">
					</el-option>
				</el-select>
			</el-form-item> -->
			<el-form-item label="存放位置" prop="storLoc">
				<el-input v-model="queryParams.storLoc" placeholder="请输入存放位置" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<!-- <el-form-item label="归档模式" prop="archMode">
				<el-select v-model="queryParams.archMode" placeholder="请选择归档模式">
					<el-option v-for="dict in arch_mode" :key="dict.value" :labebatchPrintl="dict.label" :value="dict.value">
					</el-option>
				</el-select>
			</el-form-item> -->
			<el-form-item>
				<el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
				<el-button icon="Refresh" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain icon="Plus" @click="handleBatchPrint" v-hasPermi="['base:box:batchPrint']">
					批量打印</el-button>
			</el-col>
			<!-- 	<el-col :span="1.5">
				<el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['base:box:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['base:box:edit']">修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['base:box:remove']">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['base:box:export']">导出</el-button>
			</el-col> -->
			<right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="boxList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="序号" align='center' width="100">
				<template #default="scope">
					{{ scope.$index+1 }}
				</template>
			</el-table-column>
			<el-table-column label="档案区编号" width="150" align="center" prop="areaNo" />
			<el-table-column label="档案柜" width="150" align="center" prop="cabinetNo" />
			<el-table-column label="档案盒条码" width="150" align="center" prop="boxRfid" />
			<el-table-column label="rfid" width="150" align="center" prop="rfid" />
			<el-table-column label="档案对象类型	" align="center" prop="archObjType" width="200px">
				<template #default="scope">
					<dict-tag :options="arch_obj_type" :value="scope.row.archObjType" />
				</template>
			</el-table-column>
			<el-table-column label="档案盒状态" width="150" align="center" prop="boxStatus">
				<template #default="scope">
					<dict-tag :options="box_stat" :value="scope.row.boxStatus" />
				</template>
			</el-table-column>
			<el-table-column label="借阅状态" width="150" align="center" prop="giveBackStatus">
				<template #default="scope">
					<dict-tag :options="borrow_flag" :value="scope.row.giveBackStatus" />
				</template>
			</el-table-column>
			<el-table-column label="盒号" width="150" align="center" prop="boxCode" />
			<el-table-column label="盒内户数" align="center" prop="hshdNumInBox" />
			<el-table-column label="盒规格" width="150" align="center" prop="specs" />
			<el-table-column label="总件数" align="center" prop="tNum" />
			<el-table-column label="总页数" align="center" prop="tPage" />
			<el-table-column label="库位号" width="150" align="center" prop="houseLocationNo" />
			<el-table-column label="保管期限" align="center" prop="keepDur">
				<template #default="scope">
					<dict-tag :options="keep_dur" :value="scope.row.keepDur" />
				</template>
			</el-table-column>
			<el-table-column label="保密级别" align="center" prop="confdLv">
				<template #default="scope">
					<dict-tag :options="confd_lv" :value="scope.row.confdLv" />
				</template>
			</el-table-column>
			<el-table-column label="归档部门" align="center" prop="archDept" />
			<el-table-column label="归档日志" align="center" prop="archLog" />
			<el-table-column label="已装页数" align="center" prop="pagesInNum" />
			<el-table-column label="已装袋数" align="center" prop="baggedNum" />
			<el-table-column label="客户档案盒说明" align="center" prop="tBagged" width="300px" />
			<el-table-column label="管理单位" align="center" prop="mgtOrgNo" />
			<el-table-column label="客户档案库房标识" align="center" prop="archWhId" width="150px" />
			<el-table-column label="盒满标志" align="center" prop="boxFullFlag">
				<template #default="scope">
					<dict-tag :options="box_full_flag" :value="scope.row.boxFullFlag" />
				</template>
			</el-table-column>
			<el-table-column label="客户标识" align="center" prop="custId" />
			<el-table-column label="用户编号" align="center" prop="custNo" />
			<el-table-column label="用户名称" align="center" prop="custName" />
			<el-table-column label="转存标志" align="center" prop="dumpFlag">
				<template #default="scope">
					<dict-tag :options="dump_flag" :value="scope.row.dumpFlag" />
				</template>
			</el-table-column>
			<el-table-column label="存放位置" align="center" prop="storLoc" width="300px" />
			<el-table-column label="节编号" align="center" prop="nodesNo" />
			<el-table-column label="层号" align="center" prop="layerNo" />
			<el-table-column label="位号" align="center" prop="locNo" />
			<el-table-column label="归档模式" align="center" prop="archMode">
				<template #default="scope">
					<dict-tag :options="arch_mode" :value="scope.row.archMode" />
				</template>
			</el-table-column>
			<el-table-column label="档案对象类型" align="center" prop="archObjType" width="200px">
				<template #default="scope">
					<dict-tag :options="arch_obj_type" :value="scope.row.archObjType" />
				</template>
			</el-table-column>
			<el-table-column label="档案类型" align="center" prop="mtrlType" width="200px">
				<template #default="scope">
					<dict-tag :options="archive_type" :value="scope.row.mtrlType" />
				</template>
			</el-table-column>
			<el-table-column label="是否写卡" align="center" prop="status">
				<template #default="scope">
					<dict-tag :options="is_write" :value="scope.row.status" />
				</template>
			</el-table-column>

			<el-table-column label="描述" align="center" prop="describ" width="200px" />
			<el-table-column label="操作" width="350" align="center" class-name="small-padding fixed-width" fixed="right">
				<template #default="scope">
					<el-button type="text" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['base:box:edit']">
						修改</el-button>
					<!-- <el-button type="text" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['base:box:remove']">删除</el-button> -->
					<el-button type="text" icon="Upload" @click="handleOperation(scope.row,'open')" v-hasPermi="['archive:ArchiveCabinet:edit']">开架</el-button>
					<el-button type="text" icon="Download" @click="handleOperation(scope.row,'close')" v-hasPermi="['archive:ArchiveCabinet:edit']">合架</el-button>
					<el-button type="text" icon="VideoPause" @click="handleOperation(scope.row,'stop')" v-hasPermi="['archive:ArchiveCabinet:edit']">停止</el-button>
					<el-button type="text" icon="Hide" @click="handleOperation(scope.row,'unlock')" v-hasPermi="['archive:ArchiveCabinet:edit']">解禁</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
		 @pagination="getList" />

		<!-- 添加或修改档案盒管理对话框 -->
		<el-dialog :title="title" v-model="open" width="1000px" append-to-body>
			<el-form ref="boxRef" :model="form" :rules="rules" label-width="120px">
				<el-row>
					<el-col :span="12">
						<el-form-item label="档案盒号" prop="boxCode" v-if="form.boxId==null">
							<el-input v-model="form.boxCode" placeholder="请输入盒号" style="width:220px;" maxlength="8" />
						</el-form-item>
						<el-form-item label="档案盒号" prop="boxCode" v-if="form.boxId!=null">
							<el-input v-model="form.boxCode" placeholder="请输入盒号" style="width:220px;" maxlength="8" disabled="disabled" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="档案盒条码" prop="boxRfid">
							<el-input v-model="form.boxRfid" placeholder="请输入档案盒条码" maxlength="24" style="width:220px;" />
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="12">
						<el-form-item label="档案区编号" prop="areaNo">
							<el-select v-model="form.areaNo" placeholder="请选择档案区编号" style="width:220px;">
								<el-option v-for="item in areaOptions" :key="item.archAreaNo" :label="item.archAreaNo" :value="item.archAreaNo"
								 @click="getListArchiveCabinet"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="档案柜" prop="cabinetNo">
							<el-select v-model="form.cabinetNo" placeholder="请选择档案柜" style="width:220px;">
								<el-option v-for="item in cabinetOptions" :key="item.colNo" :label="item.colNo" :value="item.colNo"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="12">
						<el-form-item label="库位号" prop="houseLocationNo">
							<el-input v-model="form.houseLocationNo" placeholder="请输入库位号" style="width:220px;" maxlength="64" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="盒规格" prop="specs">
							<el-input v-model="form.specs" placeholder="请输入盒规格" style="width:220px;" maxlength="8" />
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>

					<el-col :span="12">
						<el-form-item label="盒内户数" prop="hshdNumInBox">
							<el-input v-model="form.hshdNumInBox" placeholder="请输入盒内户数" style="width:220px;" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="总件数" prop="tNum">
							<el-input v-model="form.tNum" placeholder="请输入总件数" style="width:220px;" />
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>

					<el-col :span="12">
						<el-form-item label="总页数" prop="tPage">
							<el-input v-model="form.tPage" placeholder="请输入总页数" style="width:220px;" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="保管期限" prop="keepDur">
							<el-select v-model="form.keepDur" placeholder="请选择">
								<el-option v-for="dict in keep_dur" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>

					<el-col :span="12">
						<el-form-item label="保密级别" prop="confdLv">
							<el-select v-model="form.confdLv" placeholder="请选择">
								<el-option v-for="dict in confd_lv" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="归档部门" prop="archDept">
							<el-input v-model="form.archDept" placeholder="请输入归档部门" style="width:220px;" />
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>

					<el-col :span="12">
						<el-form-item label="归档日志" prop="archLog">
							<el-input v-model="form.archLog" placeholder="请输入归档日志" style="width:220px;" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="已装页数" prop="pagesInNum">
							<el-input v-model="form.pagesInNum" placeholder="请输入已装页数" style="width:220px;" />
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>

					<el-col :span="12">
						<el-form-item label="已装袋数" prop="baggedNum">
							<el-input v-model="form.baggedNum" placeholder="请输入已装袋数" style="width:220px;" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="客户档案盒说明" prop="tBagged">
							<el-input v-model="form.tBagged" placeholder="请输入客户档案盒说明" style="width:220px;" />
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>

					<el-col :span="12">
						<el-form-item label="管理单位" prop="mgtOrgNo">
							<el-input v-model="form.mgtOrgNo" placeholder="请输入管理单位" style="width:220px;" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="用户编号" prop="custNo">
							<el-input v-model="form.custNo" placeholder="请输入用户编号" style="width:220px;" />
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>

					<el-col :span="12">
						<el-form-item label="用户名称" prop="custName">
							<el-input v-model="form.custName" placeholder="请输入用户名称" style="width:220px;" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="存放位置" prop="storLoc">
							<el-input v-model="form.storLoc" placeholder="请输入存放位置" style="width:220px;" />
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="12">
						<el-form-item label="归档模式" prop="archMode">
							<el-select v-model="form.archMode" placeholder="请选择">
								<el-option v-for="dict in arch_mode" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="节编号" prop="nodesNo">
							<el-input v-model="form.nodesNo" placeholder="请输入节编号" style="width:220px;" />
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="12">
						<el-form-item label="层号" prop="layerNo">
							<el-input v-model="form.layerNo" placeholder="请输入层号" style="width:220px;" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="位号" prop="locNo">
							<el-input v-model="form.locNo" placeholder="请输入位号" style="width:220px;" />
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="12">
						<el-form-item label="描述" prop="describ">
							<el-input v-model="form.describ" placeholder="请输入描述" style="width:220px;" maxlength="100" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="rfid" prop="rfid">
							<el-row class="el-col-lg-14" style="margin-right: 3%;">
								<el-input v-model="form.rfid" placeholder="请输入rfid" maxlength="24" />
							</el-row>
							<el-button type="primary" plain @click="cardReading">读卡</el-button>
							<el-button type="success" plain @click="writeCard">写卡</el-button>
							<el-link :type="messagecolor"> {{rfidmessage}}</el-link>
						</el-form-item>
					</el-col>
				</el-row>

				<!-- <el-form-item label="客户档案库房标识" prop="archWhId">
					<el-input v-model="form.archWhId" placeholder="请输入客户档案库房标识" />
				</el-form-item>
				<el-form-item label="盒满标志" prop="boxFullFlag">
					<el-input v-model="form.boxFullFlag" placeholder="请输入盒满标志" />
				</el-form-item>
				<el-form-item label="客户标识" prop="custId">
					<el-input v-model="form.custId" placeholder="请输入客户标识" />
				</el-form-item>
				<el-form-item label="转存标志" prop="dumpFlag">
					<el-input v-model="form.dumpFlag" placeholder="请输入转存标志" />
				</el-form-item> -->
			</el-form>
			<template #footer>
				<div class="dialog-footer">
					<el-button type="primary" @click="submitForm">确 定</el-button>
					<el-button @click="cancel">取 消</el-button>
				</div>
			</template>
		</el-dialog>
	</div>
</template>

<script setup name="Box">
	import {
		listBox,
		getBox,
		delBox,
		addBox,
		updateBox,
		operationShelvesByBoxNo,
		batchPrint
	} from "@/api/base/box";
	import {
		getPc,
		padRight
	} from "@/assets/static/js/dataUtils.js";

	import {
		reactive,
		ref,
		onMounted,
		onBeforeUnmount,
		getCurrentInstance,
		toRefs
	} from "vue";
	import {
		listArchiveCabinetAll
	} from "@/api/archive/ArchiveCabinet";
	import {
		listArchiveArchAreaAll
	} from "@/api/archive/ArchiveArchArea";
	import {  localRequest } from '@/utils/request';
	onMounted(() => {
		init(); //开启websocket连接
	})
	onBeforeUnmount(() => {
		//USB-HID 关闭连接
		let endmsg = {
			wid: 4,
			baseOrApp: 2,
			data: {},
			msgId: 0x00,
		};
		// websocket.send(JSON.stringify({ "wid": 4, "data":{}, "baseOrApp": 2, "msgId":0x03}));
		websocket.send(JSON.stringify(endmsg));
	})

	const {
		proxy
	} = getCurrentInstance();
	const {
		is_write,
		in_out_status,
		borrow_flag,
		keep_dur,
		confd_lv,
		box_full_flag,
		arch_obj_type,
		archive_type,
		dump_flag,
		box_stat,
		arch_mode
	} = proxy.useDict('is_write', 'in_out_status', 'borrow_flag', 'keep_dur', 'confd_lv', 'box_full_flag', 'arch_obj_type',
		'archive_type', 'dump_flag', 'box_stat', 'arch_mode');
	const cabinetOptions = ref([]);
	const areaOptions = ref([]);
	const boxList = ref([]);
	const open = ref(false);
	const loading = ref(true);
	const showSearch = ref(true);
	const ids = ref([]);
	const single = ref(true);
	const multiple = ref(true);
	const total = ref(0);
	const title = ref("");
	const data = reactive({
		form: {
			areaNo: null
		},
		queryParams: {
			pageNum: 1,
			pageSize: 10,
			boxCode: null,
			boxRfid: null,
			rfid: null,
			areaNo: null,
			giveBackStatus: null,
			hshdNumInBox: null,
			tNum: null,
			tPage: null,
			keepDur: null,
			confdLv: null,
			archDept: null,
			archLog: null,
			pagesInNum: null,
			baggedNum: null,
			tBagged: null,
			mgtOrgNo: null,
			archWhId: null,
			boxFullFlag: null,
			custId: null,
			custNo: null,
			custName: null,
			dumpFlag: null,
			storLoc: null,
			nodesNo: null,
			layerNo: null,
			locNo: null,
			archMode: null,
			archObjType: null,
			mtrlType: null
		},
		rules: {
			boxCode: [{
				required: true,
				message: "盒号不能为空",
				trigger: "blur"
			}],
			areaNo: [{
				required: true,
				message: "档案区不能为空",
				trigger: "blur"
			}],
			cabinetNo: [{
				required: true,
				message: "档案柜不能为空",
				trigger: "blur"
			}],
			boxRfid: [{
				required: true,
				message: "档案盒条码不能为空",
				trigger: "blur"
			}],
			houseLocationNo: [{
				required: true,
				message: "库位号不能为空",
				trigger: "blur"
			}],
		},
	});

	//RFID读卡器定义参数
	const url = 'ws://127.0.0.1:9090/rfidService/1234';
	let isConnected = ref(false);
	let rfidmessage = ref('');
	let messagecolor = ref('info');
	let websocket;
	let temptid = ref(''); //读取标签的tid
	//RFID读卡器 end

	const {
		queryParams,
		form,
		rules
	} = toRefs(data);

	//RFID start
	function init() {
		if ('WebSocket' in window) {
			//连接WebSocket节点
			websocket = new WebSocket(url);
		} else {
			//alert('浏览器不支持webSocket');
			rfidmessage.value = '浏览器不支持webSocket';
			messagecolor.value = "warning";
		}
		// 监听WebSocket打开事件
		websocket.addEventListener('open', () => {
			//console.log('WebSocket已连接');
			isConnected.value = true;
			//USB-HID 创建连接
			let msg = {
				wid: 4,
				baseOrApp: 2,
				data: {},
				msgId: 0x03,
			};
			// websocket.send(JSON.stringify({ "wid": 4, "data":{}, "baseOrApp": 2, "msgId":0x03}));
			websocket.send(JSON.stringify(msg));
		});

		// 监听接收到消息的事件
		websocket.addEventListener('message', (event) => {
			if (isConnected.value) {
				// console.log('rec'+event.data);
				const data = JSON.parse(event.data);
				//connect
				if (data.result == 0 && data.wid == 4 && data != "Success" && data != "Success." && data.msgId ==
					3) {
					//rec{"baseOrApp":2,"data":["\\\\?\\HID#VID_03EB&PID_2421&MI_00#7&2b58d925&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}"],"msgId":3,"result":0,"wid":4}
					if (data.data[0] != null) {
						let connectmsg = {
							wid: 0,
							data: data.data[0],
							connectType: 4
						};
						// console.log('connectmsg'+JSON.stringify(connectmsg));
						websocket.send(JSON.stringify(connectmsg));
					} else {
						//alert("未扫描到usbHid设备");
						rfidmessage.value = '未扫描到usbHid设备';
						messagecolor.value = "danger";
					}
				}
				//connect success
				else if (data.result == 0 && data.wid == 0) {
					//rec{"data":"Connection successful","result":0,"wid":0}
					if (data.data == "Connection successful") {
						//alert("设备连接成功");
						rfidmessage.value = '设备连接成功';
						messagecolor.value = "primary";
						// 读写卡
					}
				} else if (data.result == 0 && data.wid == 5) //读卡后返回
				{
					//rec{"data":{"antId":1,"childAntId":0,"ctesiusLtu27":0,"ctesiusLtu31":0,
					// "epc":"E200470B00A06426717B0105","frequencyPoint":915250,"pc":13312,"phase":0,"result":0,"rssi":1,
					// "tid":"E2803821200064260153717B"},"result":0,"wid":5}
					if (data.data.epc != null) {
						form.value.rfid = data.data.epc;
						form.value.status = 1; //是否写卡 0-否 1-是
						temptid.value = data.data.tid;
						setTimeout(() => {
							pause();
						}, 100)
					}
				}
				//停止读卡成功
				else if (data.result == 0 && data.wid == 4 && (data == "Success" || data == "Success.") && data
					.msgId == 255) {
					//pause res{"baseOrApp":0,"data":"Success.","msgId":255,"result":0,"wid":4}
					console.log('停止读卡成功');
					rfidmessage.value = '停止读卡成功';
					messagecolor.value = "success";
				} else if (data.result == 0 && data.wid == 4 && data.data == "Success." && data.msgId ==
					17) //写卡后返回
				{
					//rec{"data":"Single operation complete.","result":0,"wid":6}
					//rec{"baseOrApp":0,"data":"Success.","msgId":17,"result":0,"wid":4}
					// alert('写卡成功');
					rfidmessage.value = '写卡成功';
					messagecolor.value = "success";
				} else {
					//rec{"data":"null UsbDevice not exit","result":-1,"wid":0}
					// rec{"data":"Do not reconnect","result":-1,"wid":0}
					// rec{"baseOrApp":0,"data":"Success.","msgId":16,"result":0,"wid":4}
					//rec{"data":"Receive stop instruction.","result":0,"wid":6}
					//其他错误：Single operation complete.
					//其他错误：Receive stop instruction.
					if (data.data != "Success." && data.data != "Single operation complete." && data.data !=
						"Receive stop instruction.") {
						//console.log('其他错误：'+data.data);
						rfidmessage.value = '其他错误：' + data.data;
					}
				}
			} else {
				//console.log('no con');
				rfidmessage.value = '无设备usb口连接：';
			}
		});
	}

	//RFID读卡
	function cardReading() {
		//读卡
		let readmsg = {
			wid: 4,
			baseOrApp: 0,
			data: {
				antennaEnable: 1, //1 号天线读卡
				inventoryMode: 1, //循环读取
				readTid: {
					mode: 0,
					len: 6,
				}
			},
			msgId: 16, //0x10,读 6c 消息 id
		};
		//console.log('readmsg'+JSON.stringify(readmsg));
		//readmsg{"wid":4,"baseOrApp":0,"data":{"antennaEnable":1,"inventoryMode":1,"readTid":{"mode":0,"len":6}},"msgId":80}
		websocket.send(JSON.stringify(readmsg));
	}
	//停止读取操作
	function pause() {
		let pausemsg = {
			wid: 4,
			baseOrApp: 0,
			data: {},
			msgId: 255,
		};
		//console.log("send pause"+JSON.stringify(pausemsg));
		//pause{"wid":4,"baseOrApp":0,"data":{},"msgId":255}
		//pause res{"baseOrApp":0,"data":"Success.","msgId":255,"result":0,"wid":4}
		websocket.send(JSON.stringify(pausemsg));
		//{data: 'Receive stop instruction.', result: 0, wid: 6}
	}
	//RFID写卡
	function writeCard() {
		//读取
		let writemsg = {
			wid: 4,
			baseOrApp: 0,
			data: {
				antennaEnable: 1, //天线端口
				area: 1, // 待写入的标签数据区(0，保留区；1，EPC 区；2，TID 区；3，用户数据区
				start: 1, //待写入标签数据区的字起始地址
				hexPassword: null, //访问密码（可选）
				hexWriteData: getPc(form.value.rfid) + padRight(form.value.rfid, 4),
			},
			msgId: 0x11,
		};
		console.log("temptid.value:" + temptid.value);
		if (temptid.value) { //如果TID不为null则匹配写
			writemsg.data.filter = {
				area: 2,
				bitStart: 0,
				bitLength: temptid.value.length * 4,
				hexData: temptid.value
			}
		}
		console.log('writemsg' + JSON.stringify(writemsg));
		websocket.send(JSON.stringify(writemsg));
		form.value.status = 1; //是否写卡 0-否 1-是
		// writemsg{"wid":4,"baseOrApp":0,"data":{"antennaEnable":1,"area":1,"start":1,"hexPassword":null,
		// "hexWriteData":"3000393000100010036694065400","filter":{"area":2,"bitStart":0,"bitLength":96,"hexData":"E2803821200064260153717B"}},"msgId":17}
		// index2.vue:532 rec{"baseOrApp":0,"data":"Success.","msgId":17,"result":0,"wid":4}
		// rec{"data":"Single operation complete.","result":0,"wid":6}


		// writemsg{"wid":4,"baseOrApp":0,"data":{"antennaEnable":1,"area":1,"start":1,"hexPassword":null,
		//     "hexWriteData":"3000222222222222222222222222","filter":{"area":2,"bitStart":0,"bitLength":96,"hexData":"E2003412012DFF00075E180F"}},"msgId":17}
		// index2.vue:532 rec{"baseOrApp":0,"data":"Label is missing.","msgId":17,"result":-1,"wid":4}
		//rec{"data":"Single operation complete.","result":0,"wid":6}

	}

	//RFID end
	function getList() {
		loading.value = true;
		listBox(queryParams.value).then(response => {
			boxList.value = response.rows;
			total.value = response.total;
			loading.value = false;
		});
	}

	// 取消按钮
	function cancel() {
		open.value = false;
		reset();

	}

	// 表单重置
	function reset() {
		form.value = {
			boxId: null,
			boxCode: null,
			boxName: null,
			specs: null,
			boxRfid: null,
			status: 0, //是否写卡 0-否 1-是
			describ: null,
		};
		proxy.resetForm("boxRef");
		proxy.resetForm("queryRef");
		cabinetOptions.value = [];
		areaOptions.value = [];
	}
	/** 搜索按钮操作 */
	function handleQuery() {
		queryParams.value.pageNum = 1;
		getList();
	}

	/** 重置按钮操作 */
	function resetQuery() {
		proxy.resetForm("queryRef");
		handleQuery();
	}

	// 多选框选中数据
	function handleSelectionChange(selection) {
		ids.value = selection.map(item => item.boxId);
		single.value = selection.length != 1;
		multiple.value = !selection.length;
	}

	/** 新增按钮操作 */
	function handleAdd() {
		reset();
		open.value = true;
		form.value.inStockStatus = '是';
		initAreaOptions();
		title.value = "添加档案盒管理";

	}

	/** 修改按钮操作 */
	function handleUpdate(row) {
		reset();
		initAreaOptions();
		const boxId = row.boxId || ids.value

		getBox(boxId).then(response => {
			form.value = response.data;
			open.value = true;
			title.value = "修改档案盒";
		});
	}

	/** 提交按钮 */
	function submitForm() {
		proxy.$refs["boxRef"].validate(valid => {
			if (valid) {
				if (form.value.boxRfid != null && form.value.boxRfid != "") {
					form.value.status = 1; //是否写卡 0-否 1-是
				}
				if (form.value.boxId != null) {
					updateBox(form.value).then(response => {
						proxy.$modal.msgSuccess("修改成功");
						open.value = false;
						getList();
					});
				} else {
					form.value.giveBackStatus = 2; //未借阅
					if (form.value.inStockStatus == "是") {
						form.value.inStockStatus = 1;
					} else if (form.value.inStockStatus == "否") {
						form.value.inStockStatus = 2;
					}
					addBox(form.value).then(response => {
						proxy.$modal.msgSuccess("新增成功");
						open.value = false;
						getList();
					});
				}
			}
		});
	}

	/** 删除按钮操作 */
	function handleDelete(row) {
		const boxIds = row.boxId || ids.value;
		proxy.$modal.confirm('是否确认删除档案盒管理编号为"' + boxIds + '"的数据项？').then(function() {
			return delBox(boxIds);
		}).then(() => {
			getList();
			proxy.$modal.msgSuccess("删除成功");
		}).catch(() => {});
	}

	/** 导出按钮操作 */
	function handleExport() {
		proxy.download('base/box/export', {
			...queryParams.value
		}, `档案盒管理_${new Date().getTime()}.xlsx`)
	}

	//根据档案区查询档案柜
	function changeArea() {
		cabinetOptions.value = [];
		form.value.cabinetNo = null;
		queryParams.value.cabinetNo = null;
	}
	/** 查询档案区下拉树结构 */
	function getListArchiveArchArea() {
		listArchiveArchAreaAll().then(response => {
			areaOptions.value = response.rows;
		});
	};

	/** 查询档案柜下拉树结构 */
	function getListArchiveCabinet(queryq) {
		const query = {
			archiveAreaNo: form.value.areaNo || queryParams.value.areaNo
		}
		cabinetOptions.value = [];
		// 检查查询条件是否为空
		if (!query || !query.archiveAreaNo) {
			proxy.$modal.msgError("请选择档案区编号！");
			return;
		}

		listArchiveCabinetAll(query).then(response => {
			cabinetOptions.value = response.rows;
		});
	};

	/** 初始化档案区数据 */
	function initAreaOptions() {
		// 判断档案区的数据是否存在，存在不获取，不存在则获取
		if (areaOptions.value.length === 0) {
			listArchiveArchAreaAll().then(response => {
				areaOptions.value = response.rows;
			});
		}
	};

	//批量打印
	function handleBatchPrint(row) {
		const boxIds = row.boxId || ids.value;
		console.log(boxIds);
		if (boxIds == null || boxIds =='') {
			proxy.$modal.msgError("请选择一个档案盒!");
			return
		}

		proxy.$modal.confirm('是否确认打印为"' + boxIds + '"的数据项？').then(function() {
			return batchPrint(boxIds);
		}).then(async (response) => {
			getList();
			console.log(response);
			     // 2. 发送到本地打印接口
			const localRes = await localRequest.post('/print/printBarcode',  response.data);
       		 console.log('打印成功:', localRes.data);
			proxy.$modal.msgSuccess("打印成功");
			return 0;
		}).catch(() => {});

	};

	//开架
	function handleOperation(row, type) {
		reset();
		const boxId = row.boxId || ids.value

		// 往右开
		const data = {
			"type": type,
			"boxId": boxId
		};

		operationShelvesByBoxNo(data)
	}
	initAreaOptions();
	getList();
</script>
